﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using NS.Model.Systems;
using NS.Utilities;
using NS.BLL.Systems;

public partial class _Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CookieHelper.GetValue("umobile") != null)
        {
            UserName.Visible = false;
            LabTtitle.Visible = true;
            LabUserName.Visible = true;
            LabUserName.Text = CookieHelper.GetValue("umobile");
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (Session["RandCode"] == null || !(txtYZM.Text.Trim().ToUpper().Equals(Session["RandCode"].ToString())))
        {
            FailureText.Text = "验证码错误";
        }
        else
        {
            string name = string.Empty;
            if (CookieHelper.GetValue("umobile") != null)
            {
                name = LabUserName.Text;
            }
            else
            {
                name = UserName.Text.Trim();
            }
            string pwd = PassWord.Text.Trim();

            UserDetail Model = UserDetailService.Login(name, EncryptHelper.Encrypt(pwd));
            if (!string.IsNullOrEmpty(Model.LoginTip))
            {
                FailureText.Text = Model.LoginTip;
            }
            else
            {
                CookieHelper.SetObject("uid", Model.UserID.ToString());
                CookieHelper.SetObject("umobile", Model.Mobile.ToString());
                CookieHelper.SetObject("uname", Model.TrueName);
                CookieHelper.SetObject("companyid", Model.CompanyID.ToString());
                CookieHelper.SetObject("deptid", Model.DeptID.ToString());

                Response.Redirect("Main.aspx");
            }
        }
    }
}
